/**
 * 
 */
package edu.gatech.oad.mule.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import edu.gatech.oad.mule.view.interfaces.RenderablePlayer;
import edu.gatech.oad.mule.view.interfaces.RenderablePlayer.PlayerRace;

/**
 * Displays the information for all the players as a row of player panels, each
 * devoted to a single player.
 * 
 * @author Dale Avery
 * 
 */
class PlayerInfoPanel extends JPanel {

	private class PlayerPanel extends JPanel {

		private int FONT_SIZE_NAME = 15;
		private int FONT_SIZE_INFO = 13;

		private RenderablePlayer player;

		// References necessary for updating displayed information.
		JLabel labelFood;
		JLabel labelEnergy;
		JLabel labelOre;
		JLabel labelCrystite;
		JLabel labelMoney;

		/**
		 * Constructs a new PlayerPanel. The graphics loaded for this panel are
		 * customized based on the given player's data.
		 * 
		 * @param player
		 *            The player this panel will display information for.
		 * @param dimension
		 *            The size of this panel.
		 */
		public PlayerPanel(RenderablePlayer player, Dimension dimension) {
			this.player = player;

			// Border Layout will be used for its NORTH and CENTER regions.
			setBorder(new LineBorder(Color.BLACK, 4));
			setLayout(new BorderLayout(0, 0));

			// Player's name should be displayed at the top center of the panel.
			JLabel nameLabel = new JLabel("No Player");
			nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
			nameLabel.setFont(new Font("Tahoma", Font.PLAIN, FONT_SIZE_NAME));
			add(nameLabel, BorderLayout.NORTH);

			// CENTER region contains a panel for player information.
			JPanel containerPanel = new JPanel();
			add(containerPanel, BorderLayout.CENTER);
			// Player image displayed on left, information on right.
			containerPanel.setLayout(new GridLayout(1, 2, 5, 0));

			// Obtain the image for the player.
			JLabel imageLabel = new JLabel("");
			imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
			containerPanel.add(imageLabel);

			// Information is represented as five labels, each showing info on
			// one resource.
			JPanel infoPanel = new JPanel();
			containerPanel.add(infoPanel);
			infoPanel.setLayout(new GridLayout(5, 1, 0, 0));

			labelFood = new JLabel("Food: 00");
			labelFood.setFont(new Font("Tahoma", Font.PLAIN, FONT_SIZE_INFO));
			infoPanel.add(labelFood);

			labelEnergy = new JLabel("Ener: 00");
			labelEnergy.setFont(new Font("Tahoma", Font.PLAIN, FONT_SIZE_INFO));
			infoPanel.add(labelEnergy);

			labelOre = new JLabel(" Ore: 00");
			labelOre.setFont(new Font("Tahoma", Font.PLAIN, FONT_SIZE_INFO));
			infoPanel.add(labelOre);

			labelCrystite = new JLabel("Crys: 00");
			labelCrystite
					.setFont(new Font("Tahoma", Font.PLAIN, FONT_SIZE_INFO));
			infoPanel.add(labelCrystite);

			labelMoney = new JLabel("$$ : 0000");
			labelMoney.setFont(new Font("Tahoma", Font.PLAIN, FONT_SIZE_INFO));
			infoPanel.add(labelMoney);

			// Customize panel with given player information.
			if (player != null) {
				// Display the player's name.
				nameLabel.setText(player.getName());

				// Display player's color.
				containerPanel.setBorder(new LineBorder(player.getColor(), 3,
						true));

				// Build and display an image for the player.
				imageLabel.setIcon(new ImageIcon(GraphicsLib.buildPlayerImage(player)));
			}

			this.setPreferredSize(dimension);
		}

		@Override
		public void paintComponent(Graphics g) {
			// Let UI Delegate paint first, which
			// includes background filling since
			// this component is opaque.
			super.paintComponent(g);

			if (player != null) {
				// Update player wallet labels.
				labelFood.setText("Food: " + player.getFood());
				labelEnergy.setText("Ener: " + player.getEnergy());
				labelOre.setText(" Ore: " + player.getSmithore());
				labelCrystite.setText("Crys: " + player.getCrystite());
				labelMoney.setText("$$: " + player.getMoney());
			}
		}
	}

	private static final int NUM_PANELS = 4;

	private PlayerPanel[] panels;

	/**
	 * Constructs a new info panel.
	 * 
	 * @param players
	 *            The list of all players for whom information needs to be
	 *            displayed
	 * @param dimension
	 */
	public PlayerInfoPanel(RenderablePlayer[] players, Dimension dimension) {
		panels = new PlayerPanel[NUM_PANELS];

		// Player panels should be arranged in a row of 4 panels.
		this.setLayout(new GridLayout(1, NUM_PANELS));
		this.setPreferredSize(dimension);

		// Build dimension for the panels
		Dimension playerDim = new Dimension();
		playerDim.setSize(dimension.width / 4, dimension.height);

		for (int i = 0; i < panels.length; i++) {
			if (i < players.length) {
				panels[i] = new PlayerPanel(players[i], playerDim);
			} else {
				panels[i] = new PlayerPanel(null, playerDim);
			}
			this.add(panels[i]);

		}

	}
}
